<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"
             :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="文件描述" prop="fileDescribe">
        <el-input
          v-model="queryParams.fileDescribe"
          placeholder="请输入文件描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:file:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:file:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:file:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件编号" align="center" prop="fileId" />
      <el-table-column label="文件预览" align="center" prop="filePath">
        <template slot-scope="scope">
          <el-image :src="serverPath + scope.row.fileName" style="width: 150px" />
        </template>
      </el-table-column>
      <el-table-column label="文件描述" align="center" prop="fileDescribe">
        <template slot-scope="scope">
          <el-input v-model="scope.row.fileDescribe"
                    placeholder="请输入文件描述"
                    @keyup.enter.native="fileDescribeKeyup(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="文件类型" align="center" prop="fileType" />
      <el-table-column label="文件大小" align="center" prop="fileSize">
        <template slot-scope="scope">
          {{scope.row.fileSize}} KB
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" width="100">
        <template slot-scope="scope">
          <el-input type="number" v-model="scope.row.sort" @keyup.enter.native="fileDescribeKeyup(scope.row)" />
        </template>
      </el-table-column>
<!--      <el-table-column label="使用模块" align="center" prop="useModel" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:file:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改附件管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-upload
          class="upload-demo"
          action="/prod-api/basic/file/upload"
          accept=".jpg,.jpeg,.png,.gif,.bmp,.pdf,.JPG,.JPEG,.PBG,.GIF,.BMP,.PDF"
          :headers="{'Authorization' : 'Bearer ' + getToken()}"
          :on-success="fileUploadSuccess"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="uploadList"
          list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传图片文件</div>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFile, getFile, delFile, addFile, updateFile, exportFile } from "@/api/basic/file";
import { getToken } from '@/utils/auth'
import $const from "../../../utils/websocket/ServerPath";

export default {
  name: "File",
  components: {
  },
  data() {
    return {
      serverPath: $const.WORK_SERVER,
      uploadList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 附件管理表格数据
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        filePath: null,
        fileName: null,
        fileType: null,
        fileSize: null,
        useModel: null,
        fileDescribe: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        filePath: [
          { required: true, message: "附件路径不能为空", trigger: "blur" }
        ],
        fileName: [
          { required: true, message: "附件名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getToken,
    /** 文件描述修改 */
    fileDescribeKeyup(row) {
      updateFile(row).then(resp => {
        this.msgSuccess("修改成功！")
        this.getList()
      })
    },
    /** 文件上传成功操作 */
    fileUploadSuccess(resp, file, fileList) {
      if(resp.code !== 200) {
        this.msgError(resp.msg)
      }else {
        this.msgSuccess("上传成功！")
        this.getList();
      }
    },
    /** 文件删除 */
    handleRemove(file, fileList) {
      console.log(file.response)
      let fileIds = [file.response.file.fileId];
      delFile(fileIds).then(resp => {
        this.msgSuccess("删除成功！")
        this.getList();
      })
    },
    /** 点击文件 */
    handlePreview(file) {
      console.log(file);
    },
    /** 查询附件管理列表 */
    getList() {
      this.loading = true;
      listFile(this.queryParams).then(response => {
        this.fileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.getList();
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: null,
        filePath: null,
        fileName: null,
        fileType: null,
        fileSize: null,
        useModel: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fileId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.uploadList = [];
      this.open = true;
      this.title = "添加附件管理";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fileIds = row.fileId || this.ids;
      this.$confirm('是否确认删除附件管理编号为"' + fileIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFile(fileIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有附件管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFile(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
